package com.xq.dao.impl;

import com.xq.dao.PeopleDao;
import com.xq.pojo.People;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author xq
 * @version 1.0
 * @date 2021/4/6 16:59
 * @description 人们数据接口实现层
 */
@Setter
@Repository // 将此对象注入到IOC容器中，标识是一个数据层的对象
public class PeopleDaoImpl implements PeopleDao {

    /**
     * 数据源模板
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer addPeople(People people) {
        String sql = "insert into `people`(name,age,address,create_time) values(?,?,?,?)";
        Object[] args = {people.getName(),people.getAge(),people.getAddress(),people.getCreateTime()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer updatePeople(People people) {
        String sql = "update `people` set name=?,age=?,address=?,create_time=? where id = ?";
        Object[] args = {people.getName(),people.getAge(),people.getAddress(),people.getCreateTime(),people.getId()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer deletePeopleById(Integer id) {
        String sql = "delete from `people`  where id = ?";
        Object[] args = {id};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public List<People> getAllPeople() {
        String sql = "select * from `people`";
        return jdbcTemplate.query(sql, new RowMapper<People>() {
            /**
             * 查询数据库中的表返回People数据
             * @param rs jdbc返回的结果集
             * @param i 行的索引
             * @return 返回People数据
             * @throws SQLException
             */
            @Override
            public People mapRow(ResultSet rs, int i) throws SQLException {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String address = rs.getString("address");
                int age = rs.getInt("age");
                Date createTime = rs.getTimestamp("create_time");
                return new People(id,name,address,age,createTime);
            }
        });
    }

    @Override
    public People getPeopleById(Integer id) {
        String sql = "select * from `people` where id = ?;";
        Object[] args = {id};
        return jdbcTemplate.queryForObject(sql, new RowMapper<People>() {
            /**
             * 查询数据库中的表返回People单条数据
             * @param rs jdbc返回的结果集
             * @param i 行的索引
             * @return 返回People数据
             * @throws SQLException
             */
            @Override
            public People mapRow(ResultSet rs, int i) throws SQLException {
                Integer id = rs.getInt("id");
                String name = rs.getString("name");
                String address = rs.getString("address");
                Integer age = rs.getInt("age");
                Date createTime = rs.getTimestamp("create_time");
                return new People(id,name,address,age,createTime);
            }
        },args);
    }
}
